package com.xmindguoguo.boot.modular.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xmindguoguo.boot.common.annotation.OperationLog;
import com.xmindguoguo.boot.common.constant.GlobalConstant;
import com.xmindguoguo.boot.common.enumtype.OperationLogType;
import com.xmindguoguo.boot.core.BaseController;
import com.xmindguoguo.boot.core.shiro.ShiroKit;
import com.xmindguoguo.boot.modular.system.model.TSystemDictModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemDictService;
import com.xmindguoguo.boot.util.CmUtil;
import com.xmindguoguo.boot.util.ConvertUtil;
import com.xmindguoguo.boot.util.json.JsonResult;

/**
 * 控制器 //先暂时全部小写，后面写个方法注册进来，调整大小写
 * 
 * @author xmind-果果
 * @Date 2019-01-29 17:26
 */

@Controller
@RequestMapping("/tSystemDict")
public class TSystemDictController extends BaseController {

    // 所属子模块名称+当前功能名称
    private String PREFIX = "/system/tSystemDict/";

    @Autowired
    private ITSystemDictService systemDictService;

    /**
     * 跳转列表页
     */
    @RequestMapping("toTSystemDictList")
    public String toTSystemDictList() {
        return PREFIX + "tSystemDictList";
    }

    /**
     * 查询列表数据 tree 数据时用，不用删除
     */
    @RequestMapping("tSystemDictList")
    @ResponseBody
    public Object tSystemDictList(TSystemDictModel model) {
        Wrapper<TSystemDictModel> wrapper = new EntityWrapper<>();
        /* if (ToolUtil.isNotEmpty(model.getAccount())) {
            wrapper.like("account", model.getAccount());
        }
        */
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        wrapper.orderBy("createTime", false);
        return JsonResult.sendOk(systemDictService.selectList(wrapper)); // 跳转到列表页
    }

    @RequestMapping("toTSystemDictAdd")
    public String toTSystemDictAdd() {
        return PREFIX + "tSystemDictAdd";
    }

    /**
     * 添加model
     */
    @OperationLog(logName = "添加数据字典", operationLogType = OperationLogType.INSERT)
    @RequestMapping("tSystemDictAdd")
    @ResponseBody
    public Object tSystemDictAdd(@Validated TSystemDictModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setId(CmUtil.getPkId());
        model.setStatus(GlobalConstant.MODEL_ADD);
        model.setCreateTime(new Date());
        model.setCreateUserId(ShiroKit.getUserId());
        setPids(model);
        boolean flag = systemDictService.insert(model);
        return flag ? JsonResult.sendOk("添加成功", null) : JsonResult.sendError("添加失败", null);
    }

    private void setPids(TSystemDictModel model) {
        if (model.getPId() == null) {
            model.setPId(0L);
            model.setPIds(model.getId() + "");
        }
        if (model.getPId() != 0L) {
            TSystemDictModel oldModel = systemDictService.selectById(model.getPId());
            model.setPIds(oldModel.getPIds() + "_" + model.getId());
        }
    }

    @RequestMapping("toTSystemDictUpdate")
    public String toTSystemDictUpdate(Long id) {
        TSystemDictModel model = systemDictService.selectById(id);
        request.setAttribute("model", model);
        // 查询出来对应的上级部门名称
        if (model != null && model.getPId() != 0) {
            TSystemDictModel pModel = systemDictService.selectById(model.getPId());
            request.setAttribute("showNameDom", pModel.getName());
        } else {
            request.setAttribute("showNameDom", "顶级");
        }
        return PREFIX + "tSystemDictUpdate";
    }

    /**
     * 更新model
     */
    @OperationLog(logName = "更新数据字典", operationLogType = OperationLogType.UPDATE)
    @RequestMapping("tSystemDictUpdate")
    @ResponseBody
    public Object tSystemDictUpdate(@Validated TSystemDictModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        setPids(model);
        model.setUpdateTime(new Date());
        model.setUpdateUserId(ShiroKit.getUserId());
        boolean flag = systemDictService.updateById(model);
        return flag ? JsonResult.sendOk("更新成功", null) : JsonResult.sendError("更新失败", null);
    }

    @OperationLog(logName = "删除数据字典", operationLogType = OperationLogType.DELETE)
    @RequestMapping("tSystemDictDelete")
    @ResponseBody
    public Object tSystemDictDelete(Long id) {
        TSystemDictModel model = new TSystemDictModel();
        model.setStatus(GlobalConstant.MODEL_DEL);
        model.setId(id);
        boolean flag = systemDictService.updateById(model);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

    @OperationLog(logName = "批量删除数据字典", operationLogType = OperationLogType.DELETE)
    @RequestMapping("tSystemDictBatchDel")
    @ResponseBody
    public Object tSystemDictBatchDel(String ids) {
        Long[] idsArray = ConvertUtil.StringToLongArray(ids);
        List<TSystemDictModel> list = new ArrayList<>();
        TSystemDictModel model = null;
        for (Long id : idsArray) {
            model = new TSystemDictModel();
            model.setStatus(GlobalConstant.MODEL_DEL);
            model.setId(id);
            list.add(model);
        }
        boolean flag = systemDictService.updateBatchById(list);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

    /**
     * 根据数据字典的id 查询属于这个pid=这个id 的所有的对象 ，
     * 
     * @Title getSystemDict
     * @author 于国帅
     * @date 2019年2月6日 下午12:09:54
     * @param id
     * @return Object
     */
    @RequestMapping("getSystemDict")
    @ResponseBody
    public Object getSystemDict(Long id) {
        Wrapper<TSystemDictModel> wrapper = new EntityWrapper<>();
        wrapper.eq("pid", id);
        List<TSystemDictModel> systemDictList = systemDictService.selectList(wrapper);
        return JsonResult.sendOk(systemDictList);
    }

}